package com.small.push.producer.impl;

import com.small.common.manager.redis.RedisClient;
import com.small.push.bo.PushMsg;
import com.small.push.producer.MsgProducer;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Objects;

/**
 * 发送消息
 * @author dingyou
 * @version 1.0
 * @date 2019-01-09
 */
@Component
public class RedisMsgProducer implements MsgProducer {

    private final RedisClient redisClient;

    public RedisMsgProducer(RedisClient redisClient) {
        this.redisClient = redisClient;
    }

    /**
     * 发送消息
     * @param topic
     * @param msg
     * @return
     */
    @Override
    public boolean send(String topic, PushMsg msg) {
        Long lpush = redisClient.lpush(topic, msg);
        return Objects.nonNull(lpush) && lpush > 0L;
    }

    /**
     * 发送多条消息
     * @param topic
     * @param msgs
     * @return
     */
    @Override
    public boolean send(String topic, Collection<? extends PushMsg> msgs) {
        Object[] pushMsgs = msgs.toArray(new Object[0]);
        Long lpush = redisClient.lpush(topic, pushMsgs);
        return Objects.nonNull(lpush) && lpush > 0L;
    }
}
